<?php
class vsGrid 
{
	//params not optional
	var $sql 		= '';
	var $dbName 	= '';
	var $tblName 	= '';
	var $gcon;
	var $dataSource;
	//##################################################
	var $useRecordsetForPaging = true;
	
	var $template_folder	= 'temp/';
	var $export2excel		= false;
	
	var $pages 				= 1;	//not access
	
	var $enableNextPrev		=true;
	var $showNavNum			=true;
	var $drawBorder			=true;
	var $pagingOnTopList	=true;
	var $numOfPagesShow		=5;
	
	var $enablePaging 		= false;
	var $pageSize 			= 50;
	var $pagingAlignment 	= 'right';
	var $listPageName 		= '';
	
	var $showFixRow 		= true;	//header
	var $styleOfFixRow		= '';//'border:1px outset;';
	var $showFixCol 		= false;
	var $showActionCol		= true;
	
	var $grdBorderStyle 		= 'border: 1px solid #333333';
	var $grdCellSpacing 		= 1;
	var $grdCellPadding 		= 0;
	var $grdInnerBorderWidth 	= 0;
	var $grdWidth 				= '100%';
	
	var $selectionRow 		= true;	//or cell
	var $selectionBgColor 	= '#CCFFCC';
	var $selectionFrColor 	= '#000066';
	var $selection3D 		= false;
	var $selection3DStyle 	= "";
	
	var $idxDel 			= -1;
	
	var $multiDelete		   = false;
	var $multiDeleteActionFile = '';
	var $multiDeleteButtonText = ' Xóa các dòng được chọn &raquo; ';
	
	var $arrAction = array();
	var $arrParams = array();
	
	var $arrHeader 		= array();	
	var $arrFieldName 	= array();	//$key=field name and align,$val=data type
									//ex: format of fieldname: topicname,center or topicname,left
	
	var $fixBgColor 		= '#336699';//'#d4d0c8';
	var $fixFrColor 		= '#FFFFFF';//'#000066';
	var $fixFont 			= 'Tahoma,Verdana,Arial';
	var $fixFontSize 		= 11;
	var $fixFontBold 		= false;
	var $fixRowHeight 		= 25;
	var $fixColWidth 		= 30;
	var $fixTextAlignment 	= 'center';
	
	var $mainRowBgColor 	= '#F0F0F0';
	var $mainRowFrColor 	= '#000066';
	var $altRowBgColor		= '#CCCCFF';
	var $altRowFrColor 		= '#000066';
	
	var $panelButtonClr		= '#CCCCFF';
	var $panelButtonHeight	= 40;
	
	var $normalRowFont 		= 'Tahoma,Verdana,Arial';
	var $normalFontSize 	= 11;	
	
	var $actionColBgColor 	= '#d4d0c8';//'#CBDBEF';
	var $actionColFrColor 	= '#000066';
	var $actionColWidth		= 100;
	
	var $__page = '';
	var $__order = '';
	
	var $_outlineStyle = '';
	var $_showOutLine = false;
	var $_outLineHeaderStyle = 'background-color:#336699;font-weight:bold;font-size:11px;color:#FFFFFF;padding-left:5px;height:25px';
	var $_outLineHeaderText = '';
	var $_outLineHeaderAction = '';
	
	function setOutLineAction($action) { $this->_outLineHeaderAction = $action; }
	function setOutLineHeaderCaption($text) { $this->_outLineHeaderText = $text; }
	function showOutLine($status = true) { $this->_showOutLine = $status; }

	//method
	function export2excel($path = '')
	{
		if ( !empty($path) )
			$this->template_folder = $path;
		$this->export2excel = true;
	}
	function setFixCol($show = true,$w = 30)
	{
		$this->showFixCol = $show;
		$this->fixColWidth = $w;
	}
	
	function setActionCol($show = true,$w = 100)
	{
		$this->showActionCol = $show;
		$this->actionColWidth = $w;
	}
	
	function setMultiDel($enabled = true,$act_file = '',$btn_caption = '')
	{
		$this->multiDelete = $enabled;
		$this->multiDeleteActionFile = $act_file;
		if ( !empty($btn_caption) )
			$this->multiDeleteButtonText = $btn_caption;
	}
//################################################################################################################	
	function vsGrid(&$lnk,$db,$tbl='',$ssql='')
	{
		$this->gcn = $lnk;
		$this->sql = $ssql;
		$this->dbName = $db;
		$this->tblName = $tbl;
	}
	
	function grdRender()
	{
//		$temp	= split("&",$_SERVER['QUERY_STRING']);
//		echo basename($_SERVER['SCRIPT_FILENAME']) ;	newLine();	 print_r($temp);
		$numCol = 0;
		$startCol = 0;
		if ( $this->showActionCol )	$numCol++;
		if ( $this->showFixCol )	$startCol = 1;
		$str = '
			<style>
				.__vs_eff__ { filter: blendTrans(duration=1) }
			</style>
			<script language="javascript">
			function __vs_grid_check_all__(fID, is_all)
			{
				var objF = document.all[fID];
				var num_chk = parseInt(objF.length) - 1;	//bo di nut xoa tat ca
				if ( is_all == true )	//danh dau tat ca
				{
					for ( i=0; i < num_chk; i++ )
					{
						objF.elements[i].checked = true;
					}
				}
				else if ( is_all == false )
				{
					for ( i=0; i < num_chk; i++ )
					{
						objF.elements[i].checked = false;
					}
				}
			}

			function __vsgrid_confirm_del__(url_redir)
			{
				if ( confirm("Ban co chac la muon xoa khong ?") )
					self.location = url_redir;
			}
			function __vsgrid_change_cell_style__(defColor,newColor)
			{
				obj = event.srcElement;
				if ( obj.tagName != "TD" )
					return false;
				newColor = newColor.split(",");
				defColor = defColor.split(",");
				if ( event.type == "mouseover" )
				{
					obj.style.backgroundColor = newColor[0];
					obj.style.color = newColor[1];
					obj.style.border = "' . $this->selection3DStyle . '";
					obj.style.cursor = "hand";
				}
				else
				{
					obj.style.backgroundColor = defColor[0];
					obj.style.color = defColor[1];
					obj.style.border = "' . $this->grdInnerBorderWidth . 'px solid";
					obj.style.cursor = "default";
				}
			}
			function __vsgrid_change_row_style__(scrRow,action,defColor,newColor)
			{
				//get row	
				theCells = scrRow.getElementsByTagName("td");
				defColor = defColor.split(",");
				newColor = newColor.split(",");
				if ( action == "over" )
				{
					for ( i=' . $startCol . '; i<theCells.length-' . $numCol . '; i++ )
					{
						theCells[i].style.backgroundColor = newColor[0];
						theCells[i].style.color = newColor[1];
						//theCells[i].style.cursor = "hand";
					}
				}
				else if ( action == "out" )
				{
					for ( i=' . $startCol . '; i<theCells.length-' . $numCol . '; i++ )
					{
						theCells[i].style.backgroundColor = defColor[0];
						theCells[i].style.color = defColor[1];
						theCells[i].style.cursor = "default";						
					}
				}	
			}
			</script>
		';
			
		$lenEQueryString = 0;
			
		if ( $this->enablePaging )
		{
			$current_page = (!get_magic_quotes_gpc()) ? addslashes(@$_GET['__vs_page_']) : intval(@$_GET['__vs_page_']);
			if ( $current_page <= 0 )
				$current_page = 1;

			if ( $this->useRecordsetForPaging )
			{
				$temp = split("&",@$_SERVER['QUERY_STRING']);
				$rval = '';
				while ( list($key,$val) = each($temp) ) 
				{ 
					$rval = $val; 
					$__temp = split("=",$rval);
					if ( $__temp[0] == '__vs_page_' )
					{
						$lenEQueryString += strlen($rval)+1;
						$this->__page = $rval;
					}
					else if ( $__temp[0] == '__vs_order_' )
					{
						$lenEQueryString += strlen($rval)+1;
						$this->__order = $rval;
					}
				}
				
				if ( !empty($this->__order) )
				{
					$__t = split('=',$this->__order);
					
					if ( @$_SESSION['__vs_grid_current_page__'] == $current_page )
						if ( isset($_SESSION['__vs_session_grid_order__']) )	/* Da co set session */
							if ( $_SESSION['__vs_session_grid_order__'] == strtolower($__t[1]) )	/* Click lan 2 */
								if ( $_SESSION['__vs_session_grid_order__type__'] == 'ASC' )
									$_SESSION['__vs_session_grid_order__type__'] = 'DESC';
								else
									$_SESSION['__vs_session_grid_order__type__'] = 'ASC';
							else
							{
								$_SESSION['__vs_session_grid_order__'] = strtolower($__t[1]);
								$_SESSION['__vs_session_grid_order__type__'] = 'ASC';
							}
						else /* Chua co session */
						{
							$_SESSION['__vs_session_grid_order__'] = strtolower($__t[1]);
							$_SESSION['__vs_session_grid_order__type__'] = 'ASC';
						}
					
					$this->sql = strtolower($this->sql);
					
					$__limit = '';
					$__posLimit = strpos($this->sql,"limit");
					
					if (  $__posLimit !== false )	/* Co su dung LIMIT */
						$__limit = substr($this->sql,$__pos);
					
					$__pos = strpos($this->sql,"order by");
					
					if (  $__pos !== false )	/* Da co order */
					{
						if ( $__posLimit === false )	/* Khong co su dung LIMIT */
							$this->sql .= ',' . $__t[1] . ' ' . $_SESSION['__vs_session_grid_order__type__'];
						else
						{
							$this->sql = substr($this->sql,0,$__posLimit) . ',' . $__t[1] . $_SESSION['__vs_session_grid_order__type__'] . ' ' . $__limit;
						}							
					}	
					else
					{
						$this->sql .= $__limit;
						
						$this->sql .= ' ORDER BY ' . $__t[1] . ' ' . $_SESSION['__vs_session_grid_order__type__'];
					}
				}
				
				$this->dataSource = $this->gcn->Execute($this->sql);
				
				if ( $this->dataSource->EOF )
				{
					showMsg('Không có dữ liệu !','info','center');
					return;
				}
				$this->listPageName .= "?" . substr(@$_SERVER['QUERY_STRING'],0,strlen(@$_SERVER['QUERY_STRING'])-$lenEQueryString);
				echo '<table border="0" width="100%">';
				echo '<tr><td align="' . $this->pagingAlignment . '">';
				$this->showPaging($current_page);
				echo '</td></tr>';
				echo '</table>';
			}
			else
			{
				$s_record = ($current_page-1) * $this->pageSize;
				$e_record = $s_record + $this->pageSize;

				$sql = "show table status from " . $this->dbName . " like '" . $this->tblName . "'";
				$rs_info = $this->gcn->Execute($sql);
				$num_rows = $rs_info->fields[4];	//or fields[3]
				$rs_info->close();
				
				$temp = split("&",@$_SERVER['QUERY_STRING']);
				$rval = '';
				while ( list($key,$val) = each($temp) ) 
				{ 
					$rval = $val;
					$__temp = split("=",$rval);
					if ( $temp[0] == '__vs_page_' )
					{
						$lenEQueryString += strlen($rval)+1;
						$this->__page = $rval;
					}
					else if ( $__temp[0] == '__vs_order_' )
					{
						$lenEQueryString += strlen($rval)+1;
						$this->__order = $rval;
					}
				}

				//tinh record bat dat
				if ( !empty($this->__order) )
				{
					$__t = split('=',$this->__order);
					
					if ( isset($_SESSION['__vs_session_grid_order__']) )	/* Da co set session */
						if ( $_SESSION['__vs_session_grid_order__'] == strtolower($__t[1]) )	/* Click lan 2 */
							if ( $_SESSION['__vs_session_grid_order__type__'] == 'ASC' )
								$_SESSION['__vs_session_grid_order__type__'] = 'DESC';
							else
								$_SESSION['__vs_session_grid_order__type__'] = 'ASC';
						else
						{
							$_SESSION['__vs_session_grid_order__'] = strtolower($__t[1]);
							$_SESSION['__vs_session_grid_order__type__'] = 'ASC';
						}
					else /* Chua co session */
					{
						$_SESSION['__vs_session_grid_order__'] = strtolower($__t[1]);
						$_SESSION['__vs_session_grid_order__type__'] = 'ASC';
					}				
					
					$this->sql .= ' ORDER BY ' . $__t[1] . ' ' . $_SESSION['__vs_session_grid_order__type__'];
				}
				
				echo $this->sql;
				
				$this->dataSource = $this->gcn->Execute($this->sql . " LIMIT $s_record,$e_record");
				
				if ( $this->dataSource->EOF )
				{
					showMsg('Không có dữ liệu !','info','center');
					return;
				}
				
				$this->listPageName .= "?" . substr(@$_SERVER['QUERY_STRING'],0,strlen(@$_SERVER['QUERY_STRING'])-$lenEQueryString);
				echo '<table border="0" width="100%">';
				echo '<tr><td align="' . $this->pagingAlignment . '">';
				$this->showPaging($current_page,$num_rows);
				echo '</td></tr>';
				echo '</table>';
			}
		}
		else
			$this->dataSource = $this->gcn->Execute($this->sql);
					
		if ( $this->_showOutLine )
			$str .= '
			<table cellpading="0" cellspacing="0" border="0" align="center" width="' . $this->grdWidth . '" style="' . $this->_outlineStyle . '">
				<tr>
					<td align="left" style="' . $this->_outLineHeaderStyle . '">
					' . $this->_outLineHeaderText . '
					</td>
					<td align="right" style="' . $this->_outLineHeaderStyle . '">
					' . $this->_outLineHeaderAction . '
					</td>
				</tr>
				<tr><td align="center" valign="top" colspan="2">
			';
				
		$str .=  '<table width="' . $this->grdWidth . '" border="' . $this->grdInnerBorderWidth . '" cellspacing="' . $this->grdCellSpacing . '" cellpadding="' . $this->grdCellPadding . '" style="' . $this->grdBorderStyle . '">';
		if ( $this->showFixRow == true )
		{
			$arrTemp = array();
			$arrTemp = $this->arrHeader;
			$arrF	 = $this->arrFieldName;
			$str .=  '<tr>';
			if ( $this->showFixCol == true )
				$str .=  '
					<td width="' . $this->fixColWidth . '" align="' . $this->fixTextAlignment . '" height="' . $this->fixRowHeight . '" style="' . $this->styleOfFixRow . ' background-color:' . $this->fixBgColor . ';font-family:' . $this->fixFont . ';font-size:' . $this->fixFontSize . ';color:' . $this->fixFrColor . ';font-weight:bold;">
						&nbsp;
					</td>
				';
			while ( list($key,$val) = each($arrTemp) )
			{
				list($__k,$__v)	= each($arrF);
				
				if ( is_numeric($__k) )
					$__t = split(',',$__v);
				else
					$__t = split(',',$__k);
				
				$__img = '';
				
				if ( @$_SESSION['__vs_session_grid_order__'] == strtolower($__t[0]) )
				{
					if ( @$_SESSION['__vs_session_grid_order__type__'] == 'DESC' )
						$__img = '&nbsp;<img src="images/order_desc.gif" border="0" align="absmiddle">';
					else
						$__img = '&nbsp;<img src="images/order_asc.gif" border="0" align="absmiddle">';
				}
				if ( is_numeric($key) )	//khong co do rong
				{
					$str .=  '
						<td align="' . $this->fixTextAlignment . '" height="' . $this->fixRowHeight . '" style="' . $this->styleOfFixRow . ' background-color:' . $this->fixBgColor . ';font-family:' . $this->fixFont . ';font-size:' . $this->fixFontSize . ';color:' . $this->fixFrColor . ';font-weight:bold">
							<a style="text-decoration:none; color:' . $this->fixFrColor . '" href="' . $this->listPageName . '&__vs_order_=' . $__t[0] . ( empty($this->__page) ? '' : '&' . $this->__page ) . '">' . $val . $__img . '</a>
						</td>
					';
				}
				else
				{
					$str .=  '
						<td width="' . $val . '" align="' . $this->fixTextAlignment . '" height="' . $this->fixRowHeight . '" style="' . $this->styleOfFixRow . ' background-color:' . $this->fixBgColor . ';font-family:' . $this->fixFont . ';font-size:' . $this->fixFontSize . ';color:' . $this->fixFrColor . ';font-weight:bold">
							<a style="text-decoration:none; color:' . $this->fixFrColor . '" href="' . $this->listPageName . '&__vs_order_=' . $__t[0] . ( empty($this->__page) ? '' : '&' . $this->__page ) . '">' . $key . $__img . '</a>
						</td>
					';
				}
			}
			if ( $this->showActionCol == true )
				$str .=  '
					<td width="' . $this->actionColWidth . '" align="' . $this->fixTextAlignment . '" height="' . $this->fixRowHeight . '" style="' . $this->styleOfFixRow . ' background-color:' . $this->fixBgColor . ';font-family:' . $this->fixFont . ';font-size:' . $this->fixFontSize . ';color:' . $this->fixFrColor . ';font-weight:bold">
						&nbsp;
					</td>
				';
			$str .=  '</tr>';
		}
		
		$str .= '<form name="__vs_grid_form_multi_del__" action="' . $this->multiDeleteActionFile . '" method="POST">';		
		//start load data
		unset($arrTemp);
		$autoNum = 0;
		/********************************/
		if ( !$this->enablePaging )	//not paging
		{
			$autoid = 1;
			while ( !$this->dataSource->EOF )
			{
				if ( (++$autoNum%2) == 0 )
				{
					$clr = $this->mainRowBgColor;
					$clr1 = $this->mainRowFrColor;
				}
				else
				{
					$clr = $this->altRowBgColor;
					$clr1 = $this->altRowFrColor;
				}
				if ( $this->selectionRow == true )
				{
					$str .= '<tr onMouseOver="__vsgrid_change_row_style__(this,\'over\',\'' . $clr . ',' . $clr1 . '\',\'' . $this->selectionBgColor . ',' . $this->selectionFrColor . '\',-1)" onMouseOut="__vsgrid_change_row_style__(this,\'out\',\'' . $clr . ',' . $clr1 . '\',\'' . $this->selectionBgColor . ',' . $this->selectionFrColor . '\',-1)">';
				}
				else
				{
					if ( $this->selection3D == true )
					{
						$str .= '<tr>';	
					}
				}
				if ( $this->showFixCol )
					$str .=  '
							<td id="__frow__' . $autoNum . '" align="center" height="21" style="' . $this->styleOfFixRow . ' background-color:' . $this->fixBgColor . ';font-family:' . $this->fixFont . ';font-size:' . $this->fixFontSize . ';color:' . $this->fixFrColor . ';font-weight:bold; padding: 2px 2px 2px 2px">
								' . $autoNum . '
							</td>
					';
				$arrTemp = $this->arrFieldName;
				while ( list($key,$val) = each($arrTemp) )
				{
					$dataType = '';
					if ( is_numeric($key) )
					{
						//kg co dung user func
						$dataType = '';
						$temp = split(",",$val);						
						if ( !isset($temp[1] ) )	//khong co dinh dang
							$al = 'left';
						else
							$al = $temp[1];
					}
					else
					{
						//co dung user func
						//su dung key, val = func name
						$dataType = $val;							
						$temp = split(",",$key);						
						if ( !isset($temp[1] ) )	//khong co dinh dang
							$al = 'left';
						else
							$al = $temp[1];	
					}
					if ( $this->selectionRow == true )
					{
						if ( empty($dataType) || $dataType == 'text' )
							$str .=  '
								<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
									' . $this->dataSource->Fields($temp[0]) . '
								</td>
							';
						else
							$str .=  '
								<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
									' . call_user_func($dataType,$this->dataSource->Fields($temp[0])) . '
								</td>
							';
					}
					else
					{
						if ( $this->selection3D == true )
						{
							if ( empty($dataType) || $dataType == 'text' )
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . $this->dataSource->Fields($temp[0]) . '
									</td>
								';
							else
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . call_user_func($dataType,$this->dataSource->Fields($temp[0])) . '
									</td>
								';
						}
						else
						{
							if ( empty($dataType) || $dataType == 'text' )
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . $this->dataSource->Fields($temp[0]) . '
									</td>
								';
							else
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . call_user_func($dataType,$this->dataSource->Fields($temp[0])) . '
									</td>
								';
						}
					}
					unset($temp);					
				}
				if ( $this->showActionCol == true )
				{
					$strParams = '';
					$act = $this->arrAction;
					$params = $this->arrParams;
					//get params
					while ( list($key,$val) = each($params) )
					{
						$temp = split(",",$val);
						if ( !isset($temp[1]) )
							$strParams .= $key . '=' . $this->dataSource->Fields($val) . "&";
						else if ( $temp[1] == 'f' )	//fields
							$strParams .= $key . '=' . $this->dataSource->Fields($temp[0]) . "&";
						else if ( $temp[1] == 'v' )	//variable
							$strParams .= $key . '=' . $temp[0];
					}
					
					$str .= '<td width="' . $this->actionColWidth . '" align="' . $this->fixTextAlignment . '" height="21" style="background-color:' . $this->actionColBgColor . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $this->actionColFrColor . '; padding: 2px 2px 2px 2px">';
					$stt = 1;
					while ( list($key,$val) = each($act) )
					{
						$tempP = '';
						//get params
						if ( strpos($val,"?") === false )	//khong co dau ?
							$tempP = "?unique=" . sha1('vsGrid') . "&" . $strParams;
						else
							$tempP = '&' . $strParams;
						//if ( substr($strParams,strlen($strParams)-1,strlen($strParams)) == '&' )
						//	$strParams = substr($strParams,0,strlen($strParams)-1);
						//echo $this->idxDel; echo '<br />';
						if ( $this->idxDel != $stt )
							$str .=  '
									<a href="' . $val . $tempP . '">' . $key . '</a>
							';
						else
						{
							//cho phep multi del
							if ( $this->multiDelete )
								$str .= '<input type="checkbox" name="chk_' . ($autoNum-1) . '" value="' . $val . $temP . '">';
							else
								$str .=  '
										<a href="#" onClick="__vsgrid_confirm_del__(\'' . $val . $tempP . '\')">' . $key . '</a>
								';
						}
						$stt++;
					}
					$str .= '</td>';					
				}
				$str .= '</tr>';
				
				$this->dataSource->MoveNext();
			}
		}
		else	//paging
		{
			$autoid = 1;
			while ( !$this->dataSource->EOF && $autoNum < $this->pageSize )
			{
				if ( (++$autoNum%2) == 0 )
				{
					$clr = $this->mainRowBgColor;
					$clr1 = $this->mainRowFrColor;
				}
				else
				{
					$clr = $this->altRowBgColor;
					$clr1 = $this->altRowFrColor;
				}
				if ( $this->selectionRow == true )
				{
					$str .= '<tr onMouseOver="__vsgrid_change_row_style__(this,\'over\',\'' . $clr . ',' . $clr1 . '\',\'' . $this->selectionBgColor . ',' . $this->selectionFrColor . '\',-1)" onMouseOut="__vsgrid_change_row_style__(this,\'out\',\'' . $clr . ',' . $clr1 . '\',\'' . $this->selectionBgColor . ',' . $this->selectionFrColor . '\',-1)">';
				}
				else
				{
					if ( $this->selection3D == true )
					{
						$str .= '<tr>';	
					}
				}
				if ( $this->showFixCol )
					$str .=  '
							<td id="__frow__' . $autoNum . '" align="center" height="21" style="' . $this->styleOfFixRow . ' background-color:' . $this->fixBgColor . ';font-family:' . $this->fixFont . ';font-size:' . $this->fixFontSize . ';color:' . $this->fixFrColor . ';font-weight:bold; padding: 2px 2px 2px 2px">
								' . $autoNum . '
							</td>
					';
				$arrTemp = $this->arrFieldName;
				while ( list($key,$val) = each($arrTemp) )
				{
					$dataType = '';
					if ( is_numeric($key) )
					{
						//kg co dung user func
						$dataType = '';
						$temp = split(",",$val);						
						if ( !isset($temp[1] ) )	//khong co dinh dang
							$al = 'left';
						else
							$al = $temp[1];
					}
					else
					{
						//co su dung user func
						//val = user func name
						$dataType = $val;
						$temp = split(",",$key);						
						if ( !isset($temp[1] ) )	//khong co dinh dang
							$al = 'left';
						else
							$al = $temp[1];	
					}
					if ( $this->selectionRow == true )
					{
						if ( empty($dataType) || $dataType == 'text' )
						{
							//echo $autoNum . ' .::. key= ' . $key . ' .::. datatype= ' . $dataType . ' .::. ' . $temp[0]; newLine();
							$str .=  '
								<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
									' . $this->dataSource->Fields($temp[0]) . '
								</td>
							';
						}
						else
							$str .=  '
								<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
									' . call_user_func($dataType,$this->dataSource->Fields($temp[0])) . '
								</td>
							';
					}
					else
					{
						if ( $this->selection3D == true )
						{
							if ( empty($dataType) || $dataType == 'text' )
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . $this->dataSource->Fields($temp[0]) . '
									</td>
								';
							else
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . call_user_func($dataType,$this->dataSource->Fields($temp[0])) . '
									</td>
								';
						}
						else
						{
							if ( empty($dataType) || $dataType == 'text' )
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . $this->dataSource->Fields($temp[0]) . '
									</td>
								';
							else
								$str .=  '
									<td align="' . $al . '" height="21" style="background-color:' . $clr . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $clr1 . '; padding: 2px 2px 2px 2px">
										' . call_user_func($dataType,$this->dataSource->Fields($temp[0])) . '
									</td>
								';
						}
					}
					unset($temp);					
				}
				if ( $this->showActionCol == true )
				{
					$strParams = '';
					$act = $this->arrAction;
					$params = $this->arrParams;
					//get params
					while ( list($key,$val) = each($params) )
					{
						$temp = split(",",$val);
						if ( !isset($temp[1]) )
							$strParams .= $key . '=' . $this->dataSource->Fields($val) . "&";
						else if ( $temp[1] == 'f' )	//fields
							$strParams .= $key . '=' . $this->dataSource->Fields($temp[0]) . "&";
						else if ( $temp[1] == 'v' )	//variable
							$strParams .= $key . '=' . $temp[0];
					}
					$strParams = substr($strParams,0,strlen($strParams)-1);
					$str .= '<td width="' . $this->actionColWidth. '" align="' . $this->fixTextAlignment . '" height="21" style="background-color:' . $this->actionColBgColor . ';font-family:' . $this->normalRowFont . ';font-size:' . $this->normalFontSize . ';color:' . $this->actionColFrColor . '; padding: 2px 2px 2px 2px">';
					$stt = 1;
					while ( list($key,$val) = each($act) )
					{
						$tempP = '';
						//get params
						$pos = strpos($val,'?');
						if ( $pos === false )	//khong co dau ?
							$tempP = "?unique=" . sha1('vsGrid') . "&" . $strParams;
						else
							$tempP = '&' . $strParams;
						//echo $this->idxDel; echo '<br />';
						if ( $this->idxDel != $stt )
							$str .=  '
									<a href="' . $val . $tempP . '">' . $key . '</a>
							';
						else
						{
							if ( $this->multiDelete )
								$str .=  '
										<input type="checkbox" name="chk_' . ($autoNum-1) . '" value="' . $val . $tempP . '">
								';
							else
								$str .=  '
										<a href="#" onClick="__vsgrid_confirm_del__(\'' . $val . $tempP . '\')">' . $key . '</a>
								';
						}
						$stt++;
					}
					$str .= '</td>';					
				}
				$str .= '</tr>';
				
				$this->dataSource->MoveNext();
			}
		}
			/********************************************/
		if ( $this->multiDelete )
		{
			$colspan = count($this->arrHeader);
			if ( $this->showFixCol )	$colspan++;
			if ( $this->showActionCol )	$colspan++;
			$str .= '<tr><td bgcolor="' . $this->fixBgColor . '">		<input type="hidden" name="__vs_h_total_record__" value="' . $autoNum . '">&nbsp;</td><td colspan="' . ($colspan-1) . '" align="right"><a href="javascript:__vs_grid_check_all__(\'__vs_grid_form_multi_del__\',false);">Bỏ chọn tất cả</a>&nbsp;|&nbsp;<a href="javascript:__vs_grid_check_all__(\'__vs_grid_form_multi_del__\',true);">Chọn tất cả</a>&nbsp;</td></tr>';
			$str .= '<tr><td height="' . $this->panelButtonHeight . '" bgcolor="' . $this->panelButtonClr . '" colspan="' . $colspan . '" align="right"><input type="Submit" name="__vsgrid_cmd_delete__" value="Xóa các mục được chọn &raquo;">&nbsp;</td></tr>';
		}
		$str .=  '</table>';
		$str .= '</form>';
		
		if ( $this->_showOutLine )
			$str .= '</td></tr></table>';
		
		//start render
		if ( $this->export2excel )
		{
			$fn = $this->template_folder . '__vscms_excel__.xls';
			@$file = fopen($fn,'w+'); 
			if ( !$file )
				showMsg('Không thể export sang excel file !','ex','center');
			else
			{
				fputs($file,$str,strlen($str));
				fclose($file);
				header("Location: " . $fn);
//				header('Content-Encoding: utf-8');
//				header("Content-Type:application/vnd.ms-excel");
//				header('Content-Disposition: attachment; filename="' . $fn . '"');
//				header('Pragma: no-cache');
//				unlink($fn);
			}
		}
		else
			echo $str;
	}
	//###############################
	function getPages($record_count = -1 )
	{
		if ( $record_count == -1 )
		{
			$temp = intval(($this->dataSource->RecordCount() % $this->pageSize));
			if ( $temp != 0 )
				$this->pages = (int)($this->dataSource->RecordCount()/$this->pageSize) + 1;
			else if ( $temp == 0 )
			{
				$this->pages = (int)($this->dataSource->RecordCount()/$this->pageSize);
			}
		}
		else
		{
			if ( $record_count % $this->pageSize != 0 )
				$this->pages = (int)(($record_count/$this->pageSize) + 1);
			else
				$this->pages = (int)($record_count/$this->pageSize);		
		}
	}
	/*********************************************************************************************
	function:
		Paging
	Params:
		$this->dataSource: recordset (is instance of adodb_lite (or adodb))
		$this->pageSize: num record to show in each page
		$this->listPageName: name of file list data from recordset
		$current_page: current page to show
		$numOfPagesShow: num pages in nave bar
		$enableNextPrev: =true/false to show next/prev button
		$showNavNum: =true if not show input filed, =false if show input filed
		$drawBorder: =true if nav bar has border
		$pagingOnTopList: =true if nav bar show in top list; =false if nav bar show in bottom list
		$record_count: =-1 if using recordset for pageing, =<num> if using num for paging
	*********************************************************************************************/
	function showPaging($current_page, $record_count = -1)
	{
		$_SESSION['__vs_grid_current_page__']	=	$current_page;
		//echo 'record count from showPaging function = ' . $record_count . '<br>';
		echo "<script language=\"javascript\">";
		echo "function __vsgrid_paging_change_page__(newPage){ window.location = newPage;}";
		echo "</script>";
		$total_record = 0;
		if ( $record_count == -1 )
		{
			$total_record = $this->dataSource->recordcount();
			$this->getPages();
		}
		else
		{
			$this->getPages($record_count);
			$total_record = $record_count;	
		}
		if ( $this->pages > $this->numOfPagesShow )
			$this->showNavNum = false;
		if ( $this->drawBorder == true )
			echo "<table width='' cellpadding='2' cellspacing='1' border='0' style='border: 1px solid #336699'>";
		else		
			echo "<table width='' cellpadding='2' cellspacing='1' border='0'>";
		echo " <tr>";
		echo "  <td style='font-family:Tahoma,Verdana,Arial; font-size:11px'>Có tất cả <font color=\"#FF0000\">";
		echo $this->pages;
		echo "</font> trang (tổng số mẩu tin: <font color=\"#FF0000\">" . $total_record . "</font>)</td>";
		
		if ( $current_page >= $this->pages ) $current_page = $this->pages;
		
		if ( $current_page == 0 ) $current_page = 1;
		
		//Prev and move first button
		if ( $this->enableNextPrev == true )
		{
			if ( $current_page >=2 ) {
				echo "<td bgcolor=\"#336699\" style=\"cursor: hand; font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=1')\"> |« </td>";
				echo "<td bgcolor=\"#009900\" style=\"cursor: hand; font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=" . ($current_page-1) . "')\"> « </td>";
			}
			else {
				echo "<td bgcolor='#CCCCCC' style='font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF'> |« </td>";
				echo "<td bgcolor=\"#CCCCCC\" style=\"font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF\"> « </td>";
			}
		}
		
		//Nav num button
		if ( $this->showNavNum == true ) {
			if ( $this->pages <= $this->numOfPagesShow )
				for ( $i=1 ; $i <= $this->pages; $i++ )
				{
					if ( $i == $current_page )
						echo "<td bgcolor='#336699' style='font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF'>" . $i . "</td>";
					else
						echo "<td style=\"cursor:hand; font-family:Tahoma,Verdana,Arial; font-size:11px\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=" . $i . "');\">" . $i . "</td>";
				}
			else
			{
				$half = (int)(($this->numOfPagesShow-1)/2); //=1
				if ( $half == 0 ) $half = 1;
	
				if ( $current_page < $this->pages && $current_page > 1 )
				{		
					$start_pos = $current_page-$half;
					$end_pos = $current_page+$half;
	
					if ( $this->numOfPagesShow % 2 == 0 )
						$end_pos += 1;
					
					if ( $start_pos <= 0 ) { $start_pos = 1; $end_pos += 1; }
					if ( $end_pos > $this->pages ) { $start_pos -= 1; $end_pos = $this->pages; }
					
					for ( $i = $start_pos ; $i <= $end_pos; $i++ )
						if ( $i == $current_page )
							echo "<td bgcolor='#336699' style='font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF'>" . $i . "</td>";
						else
							echo "<td style=\"cursor:hand; font-family:Tahoma,Verdana,Arial; font-size:11px\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=" . $i . "');\">" . $i . "</td>";
				}
				else if ( $current_page >= $this->pages )
					for ( $i = $this->pages-$this->numOfPagesShow+1 ; $i <= $this->pages; $i++ )
						if ( $i == $current_page )
							echo "<td bgcolor='#336699' style='font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF'>" . $i . "</td>";
						else
							echo "<td style=\"cursor:hand; font-family:Tahoma,Verdana,Arial; font-size:11px\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=" . $i . "');\">" . $i . "</td>";
				else if ( $current_page <= 1 )					
					for ( $i = 1 ; $i <= $this->numOfPagesShow; $i++ )
						if ( $i == $current_page )
							echo "<td bgcolor='#336699' style='font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF'>" . $i . "</td>";
						else
							echo "<td style=\"cursor:hand; font-family:Tahoma,Verdana,Arial; font-size:11px\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=" . $i . "');\">" . $i . "</td>";
			}
		}
		else {	//show input nav field
			echo "<td align=\"center\" bgcolor=\"#F5F5F5\" style=\"padding: 0 0 0 0\">";
			echo "<input type=\"text\" name=\"__txt_pages__\" value=\"" . $current_page . "\" size=\"1\" maxlength=\"4\" onKeyDown=\"__vsgrid_do_key_press__();\" style=\"border: 1px solid #336699;\">";
			echo "</td>";
			echo "<script language='javascript'>";
			echo "	function __vsgrid_do_key_press__() {";
			echo "		var pages = parseInt(document.all['__txt_pages__'].value);";		
			echo "		if ( event.keyCode == 13 ) {";
			echo "			document.all['__txt_pages__'].blur();";
			echo "			__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=' + pages);";
			echo "		}";
			echo "	}";
			echo "</script>";		
		}
		
		//Next and move last button
		if ( $this->enableNextPrev == true )
		{
			if ( $current_page <= ($this->pages-1) ) {
				echo "<td bgcolor=\"#009900\" style=\"cursor: hand; font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=" . ($current_page+1) . "')\"> » </td>";
				echo "<td bgcolor=\"#336699\" style=\"cursor: hand; font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF\" onClick=\"__vsgrid_paging_change_page__('" . $this->listPageName . ( empty($this->__order) ? '' : '&' . $this->__order ) . "&__vs_page_=" . $this->pages . "')\"> »| </td>";
			}
			else {
				echo "<td bgcolor=\"#CCCCCC\" style=\"font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF\"> » </td>";
				echo "<td bgcolor='#CCCCCC' style='font-family:Tahoma,Verdana,Arial; font-size:11px;color:#FFFFFF'> »| </td>";
			}
		}
		
		echo " </tr>";
		echo "</table>";
		
		if ( $this->pagingOnTopList == true && $record_count == -1 )
			$this->dataSource->Move(($current_page-1)*$this->pageSize);
	}
};
?>